package com.ywu.wiki.standard.service;

import com.ywu.boot.context.YwuContext;
import com.ywu.boot.context.YwuContextHolder;
import com.ywu.common.entity.PageList;
import com.ywu.common.page.PageService;
import com.ywu.common.result.CommonResult;
import com.ywu.common.utils.TimerIdGenerateUtil;
import com.ywu.file.sys.sdk.enter.YwuFileSysExec;
import com.ywu.wiki.standard.config.YwuWikiConfig;
import com.ywu.wiki.standard.data.YwuWikiDocRepository;
import com.ywu.wiki.standard.data.YwuWikiProjectRepository;
import com.ywu.wiki.standard.dto.YwuWikiDocDto;
import com.ywu.wiki.standard.dto.YwuWikiProjectDto;
import com.ywu.wiki.standard.dto.YwuWikiTreeDto;
import com.ywu.wiki.standard.entity.YwuWikiDocEntity;
import com.ywu.wiki.standard.entity.YwuWikiDocVersionEntity;
import com.ywu.wiki.standard.entity.YwuWikiProjectAndDocEntity;
import com.ywu.wiki.standard.entity.YwuWikiProjectEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName YwuWikiDocService
 * @Description TODO
 * @Author GroundDemo
 * @Date 2024/9/20 20:38
 * @Version 1.0
 **/
@Service
@Slf4j
public class YwuWikiDocService {
    @Autowired
    private YwuWikiDocRepository repository;

    @Autowired
    private YwuWikiDocVersionService versionService;

    @Autowired
    private YwuWikiProjectRepository ywuWikiProjectRepository;

    @Autowired
    private YwuFileSysExec ywuFileSysExec;

    @Autowired
    private YwuWikiConfig config;

    /**
    * @Author GroundDemo
    * @Description 添加WIKI文档
    * @Date 17:09 2024/9/21
    * @Param [ywuWikiDocDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> createDoc(YwuWikiDocDto ywuWikiDocDto) {
        // 构建添加参数
        YwuWikiDocEntity ywuWikiDocEntity = new YwuWikiDocEntity();
        BeanUtils.copyProperties(ywuWikiDocDto, ywuWikiDocEntity);
        // 创建的时候当前版本置为0
        ywuWikiDocEntity.setCurrentVersion(0);
        ywuWikiDocEntity.setHasNotReleased(false);
        ywuWikiDocEntity.setSortNum(0);
        ywuWikiDocEntity.setStatus(YwuWikiDocEntity.DRAFT_STATUS);
        YwuContext currentContext = YwuContextHolder.getCurrentContext();
        ywuWikiDocEntity.createInfo(currentContext.getYwuContextUser().getUserAccount());
        ywuWikiDocEntity.setTenantId(currentContext.getYwuContextTenant().getTenantId());
        ywuWikiDocEntity.setId(TimerIdGenerateUtil.nextId());
        // 执行插入数据
        repository.addYwuWikiEntity(ywuWikiDocEntity);
        return CommonResult.success(null);
    }

    /**
    * @Author GroundDemo
    * @Description 更新WIKI文档
    * @Date 17:18 2024/9/21
    * @Param [ywuWikiDocDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @Transactional
    public CommonResult<Object> updateDoc(YwuWikiDocDto ywuWikiDocDto) {
        // 查询数据，判断当时要修改的ID是否存在
        YwuWikiDocEntity search = new YwuWikiDocEntity();
        search.setId(ywuWikiDocDto.getId());
        search.setTenantId(YwuContextHolder.getCurrentContext().getYwuContextTenant().getTenantId());
        YwuWikiDocEntity queryEntity = repository.getEntity(search);
        if (Objects.isNull(queryEntity)) {
            return CommonResult.error().setErrorCode("id.is.not.exists").setMessage("更新文档不存在");
        }
        // 判断当前文档是否为已发布，如果为已发布，那么就生成一个草稿副本
        if (StringUtils.equals(queryEntity.getStatus(), YwuWikiDocEntity.DRAFT_STATUS)) {
            updateDraftDoc(ywuWikiDocDto);
        } else {
            if (StringUtils.isEmpty(ywuWikiDocDto.getDocMarkdownHtml())) {
                ywuWikiDocDto.setDocMarkdownHtml(queryEntity.getDocMarkdownHtml());
            }
            if (StringUtils.isEmpty(ywuWikiDocDto.getDocMarkdownValue())) {
                ywuWikiDocDto.setDocMarkdownValue(queryEntity.getDocMarkdownValue());
            }
            updateOnlineDoc(ywuWikiDocDto);
        }
        return CommonResult.success(null);
    }

    /**
    * @Author GroundDemo
    * @Description 更新已发布的文档 - 即生成草稿版本
    * @Date 17:29 2024/9/21
    * @Param [ywuWikiDocDto]
    * @return void
    **/
    private void updateOnlineDoc(YwuWikiDocDto ywuWikiDocDto) {
        // 构建入参
        YwuWikiDocVersionEntity ywuWikiDocVersionEntity = new YwuWikiDocVersionEntity();
        BeanUtils.copyProperties(ywuWikiDocDto, ywuWikiDocVersionEntity);
        ywuWikiDocVersionEntity.setId(null);
        ywuWikiDocVersionEntity.setResourceId(ywuWikiDocDto.getId());
        // 调用version方法插入草稿版本数据
        versionService.addDraftInfo(ywuWikiDocVersionEntity);
        // 更新当前的hasNotReleased状态
        YwuWikiDocEntity updateEntity = new YwuWikiDocEntity();
        updateEntity.setId(ywuWikiDocDto.getId());
        updateEntity.setHasNotReleased(true);
        updateEntity.updateInfo(YwuContextHolder.getCurrentContext().getYwuContextUser().getUserAccount());
        updateEntity.setSortNum(ywuWikiDocDto.getSortNum());
        repository.updateYwuWikiDocEntity(updateEntity);
    }

    /**
    * @Author GroundDemo
    * @Description 更新未发布的文档
    * @Date 17:29 2024/9/21
    * @Param [ywuWikiDocDto]
    * @return void
    **/
    private void updateDraftDoc(YwuWikiDocDto ywuWikiDocDto) {
        // 构建更新参数
        YwuWikiDocEntity ywuWikiDocEntity = new YwuWikiDocEntity();
        BeanUtils.copyProperties(ywuWikiDocDto, ywuWikiDocEntity);
        ywuWikiDocEntity.updateInfo(YwuContextHolder.getCurrentContext().getYwuContextUser().getUserAccount());
        repository.updateYwuWikiDocEntity(ywuWikiDocEntity);
    }

    /**
    * @Author GroundDemo
    * @Description 删除文档数据
    * @Date 17:46 2024/9/21
    * @Param [ywuWikiDocDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @Transactional
    public CommonResult<Object> deleteDoc(YwuWikiDocDto ywuWikiDocDto) {
        // 查询是否存在
        String tenantId = YwuContextHolder.getCurrentContext().getYwuContextTenant().getTenantId();
        YwuWikiDocEntity ywuWikiDocEntity = new YwuWikiDocEntity();
        ywuWikiDocEntity.setId(ywuWikiDocDto.getId());
        ywuWikiDocEntity.setTenantId(tenantId);
        YwuWikiDocEntity entity = repository.getEntity(ywuWikiDocEntity);
        if (Objects.isNull(entity)) {
            return CommonResult.success(null);
        }
        // 优先删除所有的草稿版本数据
        versionService.deleteByResourceId(ywuWikiDocDto.getId());
        // 删除原始表中的数据
        repository.deleteDoc(ywuWikiDocDto);
        return CommonResult.success(null);
    }

    /**
    * @Author GroundDemo
    * @Description 分页查询文档数据
    * @Date 17:51 2024/9/21
    * @Param [ywuWikiDocDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> queryPageList(YwuWikiDocDto ywuWikiDocDto) {
        ywuWikiDocDto.setTenantId(YwuContextHolder.getCurrentContext().getYwuContextTenant().getTenantId());
        // 查询分页数量
        int count = repository.queryPageCount(ywuWikiDocDto);
        // 构建分页参数
        PageService.execPageStartAndEnd(ywuWikiDocDto.getPageInfo());
        // 查询数据
        List<YwuWikiDocEntity> entityList = repository.queryPageList(ywuWikiDocDto);
        // 根据resourceId查询版本为0的所有数据，后续进行草稿数据填充
        List<String> resourceIds = entityList.stream().map(YwuWikiDocEntity::getId).collect(Collectors.toList());
        List<YwuWikiDocVersionEntity> ywuWikiDocVersionEntities = versionService.queryDraftListByResourceIds(resourceIds);
        Map<String, YwuWikiDocVersionEntity> ywuWikiDocVersionEntityMap = ywuWikiDocVersionEntities.stream().collect(Collectors.toMap(YwuWikiDocVersionEntity::getResourceId, res -> res,
                (v1, v2) -> v1));
        // 分页查询不需要文档数据，将其置空
        entityList.forEach(res -> {
            if (ywuWikiDocVersionEntityMap.containsKey(res.getId())) {
                YwuWikiDocVersionEntity entity = ywuWikiDocVersionEntityMap.get(res.getId());
                res.setDocTitle(entity.getDocTitle());
                res.setProjectId(entity.getProjectId());
            }
            res.setDocMarkdownHtml(null);
            res.setDocMarkdownValue(null);
        });
        // 构建pageList对象
        PageList<YwuWikiDocEntity> resPageList = new PageList<>(ywuWikiDocDto.getPageInfo().getPageIndex(), ywuWikiDocDto.getPageInfo().getPageSize(),
                count, entityList);
        return CommonResult.success(resPageList);
    }

    /**
    * @Author GroundDemo
    * @Description 根据文档ID查询文档数据
    * @Date 18:00 2024/9/21
    * @Param [ywuWikiDocDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> queryDocById(YwuWikiDocDto ywuWikiDocDto) {
        if (StringUtils.isEmpty(ywuWikiDocDto.getId())) {
            return CommonResult.success(new YwuWikiDocEntity());
        }
        // 根据文档ID查询文档数据
        YwuWikiDocEntity ywuWikiDocEntity = new YwuWikiDocEntity();
        ywuWikiDocEntity.setId(ywuWikiDocDto.getId());
        YwuWikiDocEntity entity = repository.getEntity(ywuWikiDocEntity);
        // 如果当前存在草稿版本，则查询草稿版本数据
        if (entity.getHasNotReleased()) {
            YwuWikiDocVersionEntity ywuWikiDocVersionEntity = new YwuWikiDocVersionEntity();
            ywuWikiDocVersionEntity.setResourceId(ywuWikiDocDto.getId());
            YwuWikiDocVersionEntity draftInfo = versionService.queryDraftInfoByResourceId(ywuWikiDocVersionEntity);
            BeanUtils.copyProperties(draftInfo, entity);
        }
        return CommonResult.success(entity);
    }

    /**
    * @Author GroundDemo
    * @Description 文档发布
    * @Date 9:25 2024/9/22
    * @Param [ywuWikiDocDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @Transactional
    public CommonResult<Object> publishDoc(YwuWikiDocDto ywuWikiDocDto) {
        // 判断当前文档是否存在
        YwuWikiDocEntity search = new YwuWikiDocEntity();
        search.setId(ywuWikiDocDto.getId());
        search.setTenantId(YwuContextHolder.getCurrentContext().getYwuContextTenant().getTenantId());
        YwuWikiDocEntity queryEntity = repository.getEntity(search);
        if (Objects.isNull(queryEntity)) {
            return CommonResult.error().setCode("id.is.not.exists").setMessage("发布文档不存在");
        }
        // 判断当前文档是否可发布
        if (!(StringUtils.equals(queryEntity.getStatus(), YwuWikiDocEntity.DRAFT_STATUS) ||
                queryEntity.getHasNotReleased())) {
            return CommonResult.error().setCode("current.doc.can.not.publish").setMessage("当前文档不可发布");
        }
        // 根据文档的草稿状态和发布状态修改文档版本信息
        if (StringUtils.equals(queryEntity.getStatus(), YwuWikiDocEntity.DRAFT_STATUS)) {
            // 当前文档为草稿状态
            publishDraftInfo(queryEntity);
        } else {
            // 当前文档为已发布状态
            publishOnlineInfo(queryEntity);
        }
        return CommonResult.success(null);
    }

    /**
    * @Author GroundDemo
    * @Description 发布已发布状态的数据
    * @Date 9:32 2024/9/22
    * @Param [queryEntity]
    * @return void
    **/
    private void publishOnlineInfo(YwuWikiDocEntity queryEntity) {
        // 草稿版本的version升级为当前版本+1，并查询后返回
        YwuWikiDocVersionEntity versionEntity = new YwuWikiDocVersionEntity();
        versionEntity.setResourceId(queryEntity.getId());
        YwuWikiDocVersionEntity draftInfoEntity = versionService.incrementVersion(versionEntity, queryEntity.getCurrentVersion() + 1);
        // 更新当前数据表中的数据状态
        YwuWikiDocEntity ywuWikiDocEntity = new YwuWikiDocEntity();
        BeanUtils.copyProperties(draftInfoEntity, ywuWikiDocEntity);
        ywuWikiDocEntity.setId(queryEntity.getId());
        ywuWikiDocEntity.setHasNotReleased(false);
        ywuWikiDocEntity.setStatus(YwuWikiDocEntity.ONLINE_STATUS);
        ywuWikiDocEntity.setCurrentVersion(queryEntity.getCurrentVersion() + 1);
        ywuWikiDocEntity.updateInfo(YwuContextHolder.getCurrentContext().getYwuContextUser().getUserAccount());
        // 更新数据
        repository.updateYwuWikiDocEntity(ywuWikiDocEntity);
    }

    /**
    * @Author GroundDemo
    * @Description 发布草稿状态的数据
    * @Date 9:31 2024/9/22
    * @Param [queryEntity]
    * @return void
    **/
    private void publishDraftInfo(YwuWikiDocEntity queryEntity) {
        // 在version表中添加一条记录
        YwuWikiDocVersionEntity versionEntity = new YwuWikiDocVersionEntity();
        BeanUtils.copyProperties(queryEntity, versionEntity);
        versionEntity.setResourceId(queryEntity.getId());
        versionEntity.setVersion(queryEntity.getCurrentVersion() + 1);
        versionService.addVersionInfo(versionEntity);
        // 更新当前数据表的状态
        queryEntity.setHasNotReleased(false);
        queryEntity.setStatus(YwuWikiDocEntity.ONLINE_STATUS);
        queryEntity.setCurrentVersion(queryEntity.getCurrentVersion() + 1);
        queryEntity.updateInfo(YwuContextHolder.getCurrentContext().getYwuContextUser().getUserAccount());
        // 更新数据
        repository.updateYwuWikiDocEntity(queryEntity);
    }

    /**
    * @Author GroundDemo
    * @Description wiki文档上传图片
    * @Date 13:28 2024/9/28
    * @Param [file]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> uploadWikiFileImage(MultipartFile file) {
        String filename = file.getOriginalFilename();
        if (StringUtils.isEmpty(filename)) {
            return CommonResult.error().setCode("fileName.is.empty");
        }
        String fileType = filename.substring(filename.lastIndexOf(".") + 1);
        // 构建新的用户名
        String fileNamePrefix = TimerIdGenerateUtil.nextId();
        String newFileName = fileNamePrefix + "." + fileType;
        File fileDir = new File(config.getTempFilePath());
        if (!fileDir.exists() && !fileDir.mkdirs()) {
            return CommonResult.error().setCode("mkdir.error");
        }
        String tempFilePath = config.getTempFilePath() + File.separator + newFileName;
        File newFile = new File(tempFilePath);
        try {
            log.debug("uploadWikiFileImage file name is {}", tempFilePath);
            FileUtils.copyInputStreamToFile(file.getInputStream(), newFile);
            Map<String, String> uploadRes = ywuFileSysExec.upload(newFile, true);
            if (uploadRes.get("code").equals("200")) {
                return CommonResult.success(uploadRes.get("fileUrl"));
            } else {
                return CommonResult.error().setCode("file.upload.error");
            }
        } catch (IOException e) {
            log.error("file.upload.error.IOException {}", e.getMessage());
            return CommonResult.error().setErrorCode("file.upload.error.IOException");
        } catch (Exception e) {
            log.error("file.upload.error.exception {}", e.getMessage());
            return CommonResult.error().setErrorCode("file.upload.error.exception");
        } finally {
            try {
                FileUtils.delete(newFile);
            } catch (IOException e) {
                log.error("delete file error {}", e.getMessage());
            }
        }
    }

    /**
    * @Author GroundDemo
    * @Description 根据项目ID查询项目下的所有文档并构建树形目录
    * @Date 10:59 2024/9/29
    * @Param [ywuWikiProjectDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> queryDocTreeListByProjectId(YwuWikiProjectDto ywuWikiProjectDto) {
        // 记录返回值对象
        YwuWikiTreeDto ywuWikiTreeDto = new YwuWikiTreeDto();
        // 根据传入的projectId查询数该项目下的所有子项目
        String tenantId = YwuContextHolder.getCurrentContext().getYwuContextTenant().getTenantId();
        List<YwuWikiProjectEntity> ywuWikiProjectEntities = ywuWikiProjectRepository.queryAllProject(tenantId);
        Map<String, YwuWikiProjectEntity> ywuWikiProjectIdMap = ywuWikiProjectEntities.stream()
                .collect(Collectors.toMap(YwuWikiProjectEntity::getId, res -> res, (v1, v2) -> v1));
        if (!ywuWikiProjectIdMap.containsKey(ywuWikiProjectDto.getId())) {
            return CommonResult.success(ywuWikiTreeDto);
        }
        Map<String, List<YwuWikiProjectEntity>> ywuWikiProjectEntityMap = ywuWikiProjectEntities.stream()
                .collect(Collectors.groupingBy(YwuWikiProjectEntity::getParentProjectId));
        Set<String> allProjectIds = new HashSet<>();

        // 递归查询所有的项目ID
        parseProjectMap(ywuWikiProjectDto.getId(), ywuWikiProjectEntityMap, allProjectIds);
        allProjectIds.add(ywuWikiProjectDto.getId());
        // 根据project_id关联查询文档
        List<YwuWikiProjectAndDocEntity> entityList = ywuWikiProjectRepository.queryWikiDocByProjectIds(tenantId, new ArrayList<>(allProjectIds));
        Map<String, List<YwuWikiProjectAndDocEntity>> entityParentProjectIdMap = entityList.stream().collect(Collectors.groupingBy(YwuWikiProjectAndDocEntity::getParentProjectId));
        Map<String, List<YwuWikiProjectAndDocEntity>> entityProjectIdMap = entityList.stream().collect(Collectors.groupingBy(YwuWikiProjectAndDocEntity::getProjectId));
        // 递归调用文档数据
        YwuWikiProjectEntity entity = ywuWikiProjectIdMap.get(ywuWikiProjectDto.getId());
        ywuWikiTreeDto.setId(entity.getId());
        ywuWikiTreeDto.setProjectId(entity.getId());
        ywuWikiTreeDto.setDocTitle(entity.getProjectName());
        ywuWikiTreeDto.setChildren(new ArrayList<>());
        Set<String> hasDoDirTreeId = new HashSet<>();
        parseSonDocList(ywuWikiTreeDto.getProjectId(), entityParentProjectIdMap, ywuWikiTreeDto, entityProjectIdMap, hasDoDirTreeId);
        List<YwuWikiTreeDto> resList = new ArrayList<>();
        resList.add(ywuWikiTreeDto);
        return CommonResult.success(resList);
    }

    /**
    * @Author GroundDemo
    * @Description 递归解析子项目下的所有文档
    * @Date 13:10 2024/9/29
    * @Param [projectId, ywuWikiProjectIdMap, entityProjectIdMap, entityParentProjectIdMap]
    * @return java.util.List<com.ywu.wiki.standard.dto.YwuWikiTreeDto>
    **/
    private void parseSonDocList(String projectId, Map<String,
            List<YwuWikiProjectAndDocEntity>> entityParentProjectIdMap, YwuWikiTreeDto ywuWikiTreeDto,
            Map<String, List<YwuWikiProjectAndDocEntity>> entityProjectIdMap, Set<String> hasDoDirTreeId) {
        // 本层级目录的数据优先
        if (entityProjectIdMap.containsKey(projectId)) {
            List<YwuWikiProjectAndDocEntity> entityList = entityProjectIdMap.get(projectId);
            for (YwuWikiProjectAndDocEntity entity : entityList) {
                YwuWikiTreeDto treeDto = new YwuWikiTreeDto();
                treeDto.setId(entity.getDocId());
                treeDto.setProjectId(entity.getProjectId());
                treeDto.setDocTitle(entity.getDocTitle());
                setChildrenList(ywuWikiTreeDto, treeDto);
            }
        }
        // 多层级目录
        if (entityParentProjectIdMap.containsKey(projectId)) {
            // 获取子项目下的所有数据
            List<YwuWikiProjectAndDocEntity> multiDirEntityList = entityParentProjectIdMap.get(projectId);
            Set<String> hasDoLastTreeIds = new HashSet<>();
            for (YwuWikiProjectAndDocEntity entity : multiDirEntityList) {
                // 多层级目录优先
                if (entityParentProjectIdMap.containsKey(entity.getProjectId())) {
                    if (!hasDoDirTreeId.contains(entity.getProjectId())) {
                        YwuWikiTreeDto dirDto = new YwuWikiTreeDto();
                        // 存在子项目信息
                        dirDto.setId(entity.getProjectId());
                        dirDto.setProjectId(entity.getProjectId());
                        dirDto.setDocTitle(entity.getProjectName());
                        parseSonDocList(entity.getProjectId(), entityParentProjectIdMap, dirDto, entityProjectIdMap, hasDoDirTreeId);
                        setChildrenList(ywuWikiTreeDto, dirDto);
                        hasDoDirTreeId.add(entity.getProjectId());
                    }
                } else {
                    if (!hasDoLastTreeIds.contains(entity.getProjectId())) {
                        List<YwuWikiProjectAndDocEntity> entityList = entityProjectIdMap.get(entity.getProjectId());
                        YwuWikiTreeDto dirDto = new YwuWikiTreeDto();
                        dirDto.setId(entityList.get(0).getProjectId());
                        dirDto.setProjectId(entityList.get(0).getProjectId());
                        dirDto.setDocTitle(entityList.get(0).getProjectName());
                        for (YwuWikiProjectAndDocEntity lastEntity : entityList) {
                            YwuWikiTreeDto treeDto = new YwuWikiTreeDto();
                            treeDto.setId(lastEntity.getDocId());
                            treeDto.setProjectId(lastEntity.getProjectId());
                            treeDto.setDocTitle(lastEntity.getDocTitle());
                            setChildrenList(dirDto, treeDto);
                        }
                        setChildrenList(ywuWikiTreeDto, dirDto);
                        hasDoLastTreeIds.add(entity.getProjectId());
                    }
                }
            }
        }
    }

    /**
    * @Author GroundDemo
    * @Description 设置子数据
    * @Date 14:12 2024/9/29
    * @Param [ywuWikiTreeDto, dirDto]
    * @return void
    **/
    private void setChildrenList(YwuWikiTreeDto ywuWikiTreeDto, YwuWikiTreeDto dirDto) {
        if (CollectionUtils.isEmpty(ywuWikiTreeDto.getChildren())) {
            List<YwuWikiTreeDto> childrenList = new ArrayList<>();
            childrenList.add(dirDto);
            ywuWikiTreeDto.setChildren(childrenList);
        } else {
            List<YwuWikiTreeDto> childrenList = ywuWikiTreeDto.getChildren();
            childrenList.add(dirDto);
        }
    }

    /**
    * @Author GroundDemo
    * @Description 递归遍历，获取所有的项目ID
    * @Date 11:48 2024/9/29
    * @Param [parentProjectId, ywuWikiProjectEntityMap, allProjectIds]
    * @return void
    **/
    private void parseProjectMap(String parentProjectId, Map<String, List<YwuWikiProjectEntity>> ywuWikiProjectEntityMap,
                                 Set<String> allProjectIds) {
        if (!ywuWikiProjectEntityMap.containsKey(parentProjectId)) {
            return;
        }
        List<YwuWikiProjectEntity> sonProjectEntityList = ywuWikiProjectEntityMap.get(parentProjectId);
        for (YwuWikiProjectEntity entity : sonProjectEntityList) {
            allProjectIds.add(entity.getId());
            parseProjectMap(entity.getId(), ywuWikiProjectEntityMap, allProjectIds);
        }
    }
}
